package com.epalmpay.service.common.impl;


import javax.annotation.Resource;

import com.epalmpay.entity.GroupProfile;
import com.epalmpay.mapper.GroupProfileMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.epalmpay.entity.GroupConfig;
import com.epalmpay.mapper.GroupConfigMapper;
import com.epalmpay.service.common.ISystemParamsSettingService;

/**
 * Created by zf on 2017/5/4.
 */
@Service
public class SystemParamsSettingImpl implements ISystemParamsSettingService {



    @Resource
    GroupConfigMapper groupConfig;
    @Resource
    private GroupProfileMapper groupProfileMapper;

    public GroupConfig getSystemParamsByGroupId(Long groupId) throws Exception {
        return groupConfig.getSystemParamsByGroupId(groupId);
    }

    public int insetSystemParamsByGroupId(GroupConfig object) throws Exception {
        GroupConfig newObj=groupConfig.getSystemParamsByGroupId(object.getGroupId());
        if (newObj!=null){
            try {
                groupConfig.deleteByPrimaryKey(newObj.getId());

            }catch (Exception e){
                e.printStackTrace();
            }

        }
        if(StringUtils.isBlank(object.getFirstName())){
        	object.setFirstName("一级代理商");
        }
        if(StringUtils.isBlank(object.getSecondName())){
        	object.setSecondName("二级代理商");
        }
        if(StringUtils.isBlank(object.getThirdName())){
        	object.setThirdName("三级代理商");
        }
        return groupConfig.insertSelective(object);
    }

    public int updateSystemParamsByGroupId(GroupConfig object) throws Exception {
    	 if(StringUtils.isBlank(object.getFirstName())){
         	object.setFirstName("一级代理商");
         }
         if(StringUtils.isBlank(object.getSecondName())){
         	object.setSecondName("二级代理商");
         }
         if(StringUtils.isBlank(object.getThirdName())){
         	object.setThirdName("三级代理商");
         }
        return groupConfig.updateByGroupId(object);
    }

    public void updateGroupProfit(GroupConfig groupConfig) {
        GroupProfile groupProfile = groupProfileMapper.selectProfileByGroupid(groupConfig.getGroupId());
        groupProfile.setExtendProfilerate(groupConfig.getExtendProfilerate());
        groupProfile.setExtendProfilerate2(groupConfig.getExtendProfilerate2());
        groupProfile.setPlatformProfilerate(groupConfig.getGroupFeeRate());
        groupProfileMapper.updateByPrimaryKeySelective(groupProfile);
    }
}
